package com.sx.micowell.framework.view.dialog;

import android.app.Activity;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.example.mvvm.rx.RxPermissionReq;
import com.example.mvvm.rx.RxViewUntil;
import com.sx.micowell.R;
import com.sx.micowell.databinding.DialogSelectPhotoBinding;
import com.sx.widget.app.dialog.BaseDialog;

public class DialogPhotoSelect {
    public static final class Builder
            extends BaseDialog.Builder<DialogPhotoSelect.Builder> {
        private OnClickSelectListener onClickSelectListener;
        private DialogSelectPhotoBinding dialogSelectPhotoBinding;

        @Override
        public void dismiss() {
            super.dismiss();
            if (dialogSelectPhotoBinding != null) {
                dialogSelectPhotoBinding.unbind();
            }
        }

        public Builder(Activity activity) {
            super(activity);
            setMarginHorizontal(30);
            setCanceledOnTouchOutside(false);
            setThemeStyle(R.style.BottomAnimStyle);
            setCancelable(true);
            setContentView(R.layout.dialog_select_photo);
            dialogSelectPhotoBinding = DataBindingUtil.bind(getContentView());
            RxViewUntil.setClickShake(dialogSelectPhotoBinding.tvItemTop, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RxPermissionReq.getInstance().reqPermission(getActivity(), new RxPermissionReq.PermissionListener() {
                        @Override
                        public void success() {
                            onClickSelectListener.onClickItemTop();
                            dismiss();
                        }

                        @Override
                        public void error() {
                            dismiss();
                        }
                    }, RxPermissionReq.PRE_WRITE, RxPermissionReq.PRE_CAMERA);
                }
            });
            RxViewUntil.setClickShake(dialogSelectPhotoBinding.tvItemBottom, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RxPermissionReq.getInstance().reqPermission(getActivity(), new RxPermissionReq.PermissionListener() {
                        @Override
                        public void success() {
                            onClickSelectListener.onClickItemBottom();
                            dismiss();
                        }

                        @Override
                        public void error() {
                            dismiss();
                        }
                    }, RxPermissionReq.PRE_WRITE);

                }
            });
            RxViewUntil.setClickShake(dialogSelectPhotoBinding.tvCancel, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        public Builder setOnClickSelectListener(OnClickSelectListener onClickSelectListener) {
            this.onClickSelectListener = onClickSelectListener;
            return this;
        }
    }

    public interface OnClickSelectListener {
        void onClickItemTop();

        void onClickItemBottom();

    }
}
